package org.paradise.dms.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

/**
 * 
 * Description: DMS系统所有action的基类
 * 
 * Copyright (c) paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 Mar 4, 2009 10:33:07 PM 李双江（paradise.lsj@gmail.com）created
 */
@Service
public class DMSBaseAction extends ActionSupport implements Serializable, Preparable, ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = -330155342034188080L;
	@SuppressWarnings("unused")
	private HttpServletRequest request;

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public void outJsonString(String str) {
		getResponse().setContentType("text/javascript;charset=UTF-8");
		outString(str);
	}

	public void outJson(Object obj) {
		outJsonString(JSONObject.fromObject(obj).toString());
	}

	public void outJsonArray(Object array) {
		outJsonArray(JSONArray.fromObject(array).toString());
	}

	public void outString(String str) {
		try {
			PrintWriter out = getResponse().getWriter();
			out.write(str);
		} catch (IOException e) {
		}
	}

	public void outXMLString(String xmlStr) {
		getResponse().setContentType("application/xml;charset=UTF-8");
		outString(xmlStr);
	}

	public void prepare() throws Exception {

	}

	public void setServletRequest(HttpServletRequest arg0) {

	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

}
